package mysql

import (
	"fmt"
	"go.uber.org/zap"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"week2d/appconfig"
	"week2d/models/global"
)

func InitMysql() {
	err := global.Err
	res := appconfig.AppData.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		res.Name, res.Pwd, res.Head, res.Port, res.Dbname)
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		zap.S().Info("数据库链接失败", err)
		return
	}
	fmt.Println("数据库链接成功", res)
	//自动迁移
	global.DB.AutoMigrate(&GoodsWeek2d{}, &GoodsDetailWeek2d{})
}
